package 二叉树相关问题;

import java.util.ArrayList;
import java.util.List;

public class 二叉树的所有路径 {

    public List<String> binaryTreePaths(TreeNode root) {
        List<String> list = new ArrayList<>();
        traverse(root,"",list);
        return list;
    }

    private void traverse(TreeNode root, String path, List<String> list) {
        if (root==null)
            return;
        StringBuilder sb = new StringBuilder(path);
        sb.append(root.val);
        if (root.right==null && root.left==null) {
            list.add(sb.toString());
        }
        else{
            sb.append("->");
            traverse(root.left,sb.toString(),list);
            traverse(root.right,sb.toString(),list);
        }
    }
}
